package com.srit.core.struts2.action;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import com.srit.core.bean.Page;
import com.srit.core.bean.PageMsg;
import com.srit.core.bean.SearchParam;
import com.srit.core.constant.SystemConstant;
import com.srit.core.exception.DataNotFoundException;

/**
 * @author guan
 * 
 */
public class BaseAction extends ActionSupport implements ServletRequestAware,
		ServletResponseAware, SessionAware, Preparable, Validateable,
		ModelDriven {

	public static final String JSON = "json";
	public static final String LIST = "list";
	public static final String FORM = "form";
	public static final String ACTION = "action";
	public static final String VIEW = "view";
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected Page page = new Page();
	protected Map session;
	protected String uploadPath = ServletActionContext.getServletContext()
			.getRealPath(SystemConstant.FILE_UPLOAD_PATH);
	protected String downloadPath = ServletActionContext.getServletContext()
			.getRealPath(SystemConstant.FILE_DOWNLOAD_PATH);

	protected Logger log = LoggerFactory.getLogger(this.getClass());
	protected PageMsg pageMsg = new PageMsg();// 提示
	private static final String PACKAGEHEADER = "package ";
	private static final String ACTIONCLASSEND = "action";
	private String actionName;// acetion name;
	private String nameSpace;// action namespace;
	protected SearchParam<String, String> searchParam = new SearchParam();// 查询条件
	protected String url;// 跳转的页面
	private List result;

	public void setActionName(String actionName) {
		this.actionName = actionName;
	}

	public String getNameSpace() {
		String className = this.getClass().getName();
		String namespaceAndName = className.substring(className
				.indexOf("action.") + 7);
		if (namespaceAndName.indexOf(".") > 0) {
			nameSpace = "/"
					+ namespaceAndName.substring(0,
							namespaceAndName.indexOf("."));
		} else {
			nameSpace = "/";
		}

		return nameSpace;
	}

	public void setNameSpace(String nameSpace) {
		this.nameSpace = nameSpace;
	}

	public String getActionName() {
		int packageLength = this.getClass().getPackage().toString().length()
				- PACKAGEHEADER.length() + 1;
		String allName = this.getClass().getName();
		return allName.substring(packageLength,
				allName.length() - ACTIONCLASSEND.length()).toLowerCase();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param response
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletResponseAware#setServletResponse
	 * (javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param request
	 * 
	 * @see
	 * org.apache.struts2.interceptor.ServletRequestAware#setServletRequest(
	 * javax.servlet.http.HttpServletRequest)
	 */
	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

	public HttpServletRequest getServletRequest() {
		return request;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @throws Exception
	 * 
	 * @see com.opensymphony.xwork2.Preparable#prepare()
	 */
	@Override
	public void prepare() throws Exception {
		System.out.println("------" + ActionContext.getContext().getName());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @param session
	 * 
	 * @see
	 * org.apache.struts2.interceptor.SessionAware#setSession(java.util.Map)
	 */
	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@org.apache.struts2.json.annotations.JSON(serialize = false)
	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@org.apache.struts2.json.annotations.JSON(serialize = false)
	public PageMsg getPageMsg() {
		return pageMsg;
	}

	public SearchParam<String, String> getSearchParam() {
		return searchParam;
	}

	public void setSearchParam(SearchParam<String, String> searchParam) {
		this.searchParam = searchParam;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public void setPageMsg(PageMsg pageMsg) {
		this.pageMsg = pageMsg;
	}

	public List getResult() {
		return result;
	}

	public void setResult(List result) {
		this.result = result;
	}

	/**
	 * 对捕获的异常进行处理
	 * 
	 * @param e
	 */
	protected void catchException(Exception e) {
		pageMsg.setErrorMsg(e.getMessage());
		if (e instanceof InstantiationException) {

		} else if (e instanceof IllegalAccessException) {

		} else if (e instanceof ClassNotFoundException) {

		} else if (e instanceof SQLException) {

		} else if (e instanceof DataNotFoundException) {

		} else {
			pageMsg.setErrorMsg(e.getClass().getName() + "未处理!");
		}
	}

	@Override
	public Object getModel() {
		return null;
	}
}
